import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.swing.*;

public class Screen extends JPanel implements Runnable {
	public Thread thread = new Thread(this);
	public static boolean isFirst = true;
	public static int myWidth, myHeight;
	public static Image[] tileset_ground = new Image[100];
	public static Image[] tileset_air = new Image[100];
	public static Image[] tileset_res = new Image[100];
	public static Image[] tileset_mob =  new Image[100];
	
	public static Room room;
	public static Save save;
	public static Store store;
	
	public static int coinage = 100;
	public static int health = 100;
	
	public static Point mse = new Point(0,0);
	
	public static Mob[] mobs = new Mob[100];
	
	public static boolean isDebug = true;

	public Screen( Frame frame) {
		frame.addMouseListener(new KeyHandel());
		frame.addMouseMotionListener(new KeyHandel());
		thread.start();
		System.out.println("jestem w screen");
	}
	
	public void define() {
		room = new Room();
		save = new Save();
		store = new Store();
		
		
		
		for(int i = 0; i < tileset_ground.length;i++) {
			tileset_ground[i] = new ImageIcon("res/tileset_ground.png").getImage();
			tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, 26 * i, 26,26)));
		}
		for(int i = 0; i < tileset_air.length;i++) {
			tileset_air[i] = new ImageIcon("res/tileset_air.png").getImage();
			tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(), new CropImageFilter(0, 26 * i, 26,26)));
		}
		
		tileset_res[0] = new ImageIcon("res/tileset_res.png").getImage();
		tileset_res[1] = new ImageIcon("res/health.png").getImage();
		tileset_res[2] = new ImageIcon("res/coin.png").getImage();
		
		tileset_mob[0] = new ImageIcon("res/mob.png").getImage();
		save.loadSave(new File("save/mission1.neko"));
		
		for(int i = 0; i < mobs.length;i++) {
			mobs[i] = new Mob();
			//mobs[i].spawnMob(0);
		}
	}

	public void paintComponent(Graphics g) {
		
		if(isFirst) {
			myHeight = getHeight();
			myWidth = getWidth();
			define();
			
			isFirst = false;
		}
		
		g.setColor(new Color(70,70,70));
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(new Color(0,0,0));
		g.drawLine(room.block[0][0].x-1, 0, room.block[0][0].x-1, room.block[room.worldHeight-1][0].y + room.blockSize);
		g.drawLine(room.block[0][room.worldWidth-1].x + room.blockSize, 0, room.block[0][room.worldWidth-1].x + room.blockSize, room.block[room.worldHeight-1][0].y + room.blockSize);
		g.drawLine(room.block[0][0].x, room.block[room.worldHeight-1][0].y + room.blockSize, room.block[0][room.worldWidth-1].x + room.blockSize, room.block[room.worldHeight-1][0].y + room.blockSize);
		room.draw(g);
		
		for(int i =0; i < mobs.length;i++) {
			if (mobs[i].inGame) {
				mobs[i].draw(g);
			}
		}
		store.draw(g);
		if (health < 1) {
			g.setColor(new Color(240,40,20));
			g.fillRect(0, 0, myWidth, myHeight);
			g.setColor(new Color(255,255,255));
			g.setFont(new Font("Courier New", Font.BOLD, 14));
			g.drawString("Ahahahhah n00p!", 50, 50);
			
		}
 	}
	
	public int spawnTime = 2400, spawnFrame = 0;
	public void mobSpawner() {
		if (spawnFrame >= spawnTime) {
			for(int y =0; y <  mobs.length; y++) {
				if(!mobs[y].inGame) {
					mobs[y].spawnMob(Value.mobGreeny);
					break;
				}
			}
			spawnFrame = 0;
		} else {
			spawnFrame += 1;
		}
	}

	public void run() {
		while (true) {
			
			if(!isFirst && health > 0) {
				room.physic();
				mobSpawner();
				for(int i=0; i< mobs.length; i++) {
					if(mobs[i].inGame) {
						mobs[i].physic();
					}
				}
			}
			
			repaint();

			try {
				Thread.sleep(1);
			} catch (Exception e) {

			}
		}
	}

}
